﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace sentence
{
    class SentenceUpdater
    {
        private XmlDocument xmldoc;
        private XmlNode node;
        private String OldVersionDate = "20120101";
        private String ServerXML = "http://sentence.sinaapp.com/update.xml";
        private String NewVersionDate;
        public SentenceUpdater()
        {
            Thread Thread_CheckUpdate = new Thread(new ThreadStart(CheckUpdate));
            Thread_CheckUpdate.IsBackground = true;
            Thread_CheckUpdate.Start();
        }

        //判断网络连接
        [DllImport("wininet")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

        public void CheckUpdate()
        {
            try
            {
                while (true)
                {
                    int i = 0;
                    if (InternetGetConnectedState(out i, 0))
                    {
                        break;
                    }
                    Thread.Sleep(180 * 1000);
                }

                while (true)
                {
                    if (!IsUpdate())
                    {
                        break;
                    }
                    Thread.Sleep(180 * 1000);
                }

                if (File.Exists(Application.StartupPath + @"\updater.exe"))
                {
                    Process.Start(Application.StartupPath + @"\updater.exe", "update");
                }
                else
                {
                    BUGReport reporter = new BUGReport("自动启动后更新故障");
                    reporter.ReportSend();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("软件发生了一个错误，\r\n请发送错误报告以便我们修复这个BUG", "SORRY%>_<%");
                BUGReport reporter = new BUGReport(ex.ToString());
                reporter.ReportSend();
            }
            
        }

        public bool IsUpdate()
        {
            try
            {
                //读取客户端版本号 如果没有文件 则默认为最低版本
            xmldoc = new XmlDocument();
            if (File.Exists(Application.StartupPath + @"\app.xml"))
            {
                xmldoc.Load(Application.StartupPath + @"\app.xml");
                node = xmldoc.GetElementsByTagName("VersionDate").Item(0);
                OldVersionDate = node.InnerText.ToString();
                node = xmldoc.GetElementsByTagName("XML").Item(0);
                ServerXML = node.InnerText.ToString();
                node = xmldoc.GetElementsByTagName("Version").Item(0);
                SentenceUtility.Version = node.InnerText.ToString();
            }
            //读取网上的版本号
            xmldoc.Load(ServerXML);
            node = xmldoc.GetElementsByTagName("VersionDate").Item(0);
            NewVersionDate = node.InnerText.ToString();

            if ((int.Parse(NewVersionDate)) > (int.Parse(OldVersionDate)))
                return false;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("软件发生了一个错误，\r\n请发送错误报告以便我们修复这个BUG", "SORRY%>_<%");
                BUGReport reporter = new BUGReport(ex.ToString());
                reporter.ReportSend();
            }
            return true;
            
        }
    }
}
